﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using Meqs.Entity;
using Meqs.Entity.Views;
using Meqs.Logic;
using Meqs.Logic.Interface;

public partial class Credit_Teacher_ScoreInput_Overview : ContainerBasePage
{
    private int evalId;
    private int evalType;
    private int attribute;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack) return;
        if (!Int32.TryParse(Request["id"], out evalId))
            evalId = 0;
        if (!Int32.TryParse(Request["evaltype"], out evalType))
            evalType = 0;
        if (!Int32.TryParse(Request["attribute"], out attribute))
            attribute = 0;

        InitDisplay();
    }

    /// <summary>
    /// 初始化界面标签元素的显示行为
    /// </summary>
    protected void InitDisplay()
    {
        // 学生相关评估
        if (evalType == 1)
        { 
            lbTitle.Text = "";
            
            IList<ClsAndCauseView> clsList = Container<IClsInfoLogic>.Instance.GetClsInTeacher(SchoolMark, Common.GetUserInfo().UID);

            IList<int> idList = new List<int>();
            foreach (ClsAndCauseView info in clsList)
            {
                idList.Add(info.RID);
            }
            IList<StuCauseEvalState> stateList =
               Container<IStuCauseEvalHistoryLogic>.Instance.GetState(SchoolCommon.Sch_SchoolInfo.SchoolYear,
                                                                   SchoolCommon.Sch_SchoolInfo.SchoolTerm,
                                                                   SchoolMark, idList);

            IList<ClassResultExamList> examList;
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", SchoolMark);
            ht.Add("SchoolYear", SchoolCommon.Sch_SchoolInfo.SchoolYear);
            ht.Add("Term", SchoolCommon.Sch_SchoolInfo.SchoolTerm);
            //ht.Add("CauseID", nSubject);
            examList = Container<IClassResultExamListLogic>.Instance.FindAll(ht);

            CreateStudentEvalTable(clsList, stateList, examList, 
                SchoolCommon.Sch_SchoolInfo.SchoolYear,
                SchoolCommon.Sch_SchoolInfo.SchoolTerm);

            lbTitle.Text = "本学期评价记录";
        }
        // 班级相关评估
        else
        {
            lbTitle.Text = "";
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", SchoolMark);
            ht.Add("IsEffect", "1");
            if (evalType == 1)
                ht.Add("HeaderID", Common.GetUserInfo().UID);
            IList<ClsInfo> clsList = Container<IClsInfoLogic>.Instance.FindAll(ht);

            IList<int> idList = new List<int>();
            foreach (ClsInfo info in clsList)
            {
                idList.Add(info.RID);
            }

            ht = new Hashtable();
            ht.Add("SchoolYear", SchoolCommon.Sch_SchoolInfo.SchoolYear);
            ht.Add("Term", SchoolCommon.Sch_SchoolInfo.SchoolTerm);
            IList<TermInfo> infoList = Container<ITermInfoLogic>.Instance.FindAll(ht);
            if (infoList == null || infoList.Count < 1)
                return;
            DateTime termStart = (DateTime)infoList[0].StartDate;

            IList<ClassEvalState> stateList =
               Container<IClassEvalHistoryLogic>.Instance.GetState(SchoolCommon.Sch_SchoolInfo.SchoolYear,
                                                                   SchoolCommon.Sch_SchoolInfo.SchoolTerm,
                                                                   SchoolMark, evalId, termStart, new DateTime(DateTime.Now.Year, DateTime.Now.Month + 1, 1));
            CreateClassEvalTable(clsList, stateList,
                SchoolCommon.Sch_SchoolInfo.SchoolYear,
                SchoolCommon.Sch_SchoolInfo.SchoolTerm, termStart);

            lbTitle.Text = "本学期评价记录";
        }
    }


    /// <summary>
    /// 构造 学生相关评估 结果表格
    /// </summary>
    private void CreateStudentEvalTable(IList<ClsAndCauseView> classInfo, IList<StuCauseEvalState> stateInfo, IList<ClassResultExamList> examList,
                            int year, int term)
    {
        // 判断规则是否确认
        Hashtable ht = new Hashtable();
        ht.Add("SchoolYear", SchoolCommon.Sch_SchoolInfo.SchoolYear);
        ht.Add("Term", term);
        ht.Add("SchoolMark", SchoolMark);
        IList<StuCauseEvalHistory> schList = Container<IStuCauseEvalHistoryLogic>.Instance.FindAll(ht);

        int width = 160+75;
        TableHeaderRow hrow = new TableHeaderRow();
        TableHeaderCell hcell = new TableHeaderCell();
        hcell.Text = "班级";
        hcell.Style[HtmlTextWriterStyle.Width] = "90px";
        hrow.Cells.Add(hcell);

        hcell = new TableHeaderCell();
        hcell.Text = "科目";
        hcell.Style[HtmlTextWriterStyle.Width] = "70px";
        hrow.Cells.Add(hcell);

        IList<DateTime> dateList = new List<DateTime>();
        ListSort<StuCauseEvalState> sort = new ListSort<StuCauseEvalState>(stateInfo, "StartTime", false);
        stateInfo = sort.Sort();

        ht = new Hashtable();
        ht.Add("SchoolYear", year);
        ht.Add("Term", term);
        IList<TermInfo> infoList = Container<ITermInfoLogic>.Instance.FindAll(ht);
        if (infoList == null || infoList.Count < 1)
            return;
        DateTime termStart = (DateTime)infoList[0].StartDate;

        while (!((termStart.Month > DateTime.Now.Month || termStart.Year > DateTime.Now.Year) && termStart > DateTime.Now))
        {

            hcell = new TableHeaderCell();
            hcell.Text = String.Format("{0}月", termStart.Month);
            hcell.Style[HtmlTextWriterStyle.Width] = "75px";
            width += 75;
            hrow.Cells.Add(hcell);
            dateList.Add(termStart);
            termStart = termStart.AddMonths(1);
        }

        // 添加期末成绩列
        hcell = new TableHeaderCell();
        hcell.Text = "期末成绩";
        hcell.Style[HtmlTextWriterStyle.Width] = "75px";
        //cell.Attributes.Add("Date", state.StartTime.ToShortDateString());
        hrow.Cells.Add(hcell);

        tbResult.Rows.Add(hrow);
        tbResult.Style[HtmlTextWriterStyle.Width] = String.Format("{0}px", width);

        // 填充表内容
        TableRow row;
        TableCell cell;
        CauseFormula cf = new CauseFormula(SchoolMark, 0);
        foreach (ClsAndCauseView info in classInfo)
        {
            row = new TableRow();
            cell = new TableCell();
            cell.Text = info.ClsName;
            row.Cells.Add(cell);

            cell = new TableCell();
            cell.Text = info.CauseName;
            row.Cells.Add(cell);

            foreach (DateTime dt in dateList)
            {
                cell = new TableCell();
                bool sign = false;
                bool isOk = true;
                foreach (StuCauseEvalHistory history in schList)
                {
                    if (history.Status >= 1 || info.CauseID != history.CauseID) continue;
                    // 构造超链html标签
                    HyperLink control = new HyperLink();
                    control.Text = "未录入";
                    control.NavigateUrl = "msgPage.html";
                    cell.Controls.Add(control);
                    isOk = false;
                    break;
                }
                if (isOk)
                {
                    foreach (StuCauseEvalState state in stateInfo)
                    {
                        DateTime limitDate = new DateTime(state.StartTime.Year, state.StartTime.Month, 1);
                        limitDate = limitDate.AddMonths(1);
                        limitDate = limitDate.AddDays(cf.DayLimit);

                        if (state.ClassID != info.RID || state.StartTime.Month != dt.Month ||
                            info.CauseID != state.CauseID) continue;
                        // 构造超链html标签
                        HyperLink control = new HyperLink();
                        control.Text = "已录入";
                        if (cf.DayLimit == 0 || DateTime.Now < limitDate)
                        {
                            control.Attributes.Add("onclick", "detailView(1);");

                            control.NavigateUrl = String.Format("../ScoreInput/ScoreInput.aspx?SchoolYear={0}&Term={1}&ClsID={2}&Year={3}&Month={4}&CauseID={5}&Type=Edit",
                                                                year, term,
                                                                state.ClassID, state.StartTime.Year, state.StartTime.Month, state.CauseID);
                        }
                        cell.Controls.Add(control);

                        sign = true;
                        break;
                    }
                    if (!sign)
                    {
                        DateTime limitDate = new DateTime(dt.Year, dt.Month, 1);
                        limitDate = limitDate.AddMonths(1);
                        limitDate = limitDate.AddDays(cf.DayLimit);

                        // 构造超链html标签
                        HyperLink control = new HyperLink();
                        control.Text = "未录入";
                        if (cf.DayLimit == 0 || DateTime.Now < limitDate)
                        {
                            control.Attributes.Add("onclick", "detailView(1);");
                            control.NavigateUrl = String.Format("../ScoreInput/ScoreInput.aspx?SchoolYear={0}&Term={1}&ClsID={2}&Year={3}&Month={4}&CauseID={5}&Type=Edit",
                                                    year, term,
                                                    info.RID, dt.Year, dt.Month, info.CauseID);
                        }
                        cell.Controls.Add(control);
                    }
                }

                row.Cells.Add(cell);
            } 

            // 期末成绩 链接
            cell = new TableCell();
            bool sign1 = false;
            HyperLink scoreLink = new HyperLink();
            if (examList != null)
            {
                foreach (ClassResultExamList state in examList)
                {
                    if (state.ClassID != info.RID || info.CauseID != state.CauseID) continue;
                    // 构造超链html标签
                    sign1 = true;
                    scoreLink.Text = "已录入";
                    break;
                }
            }
            if (!sign1)
                scoreLink.Text = "未录入";
            
            scoreLink.NavigateUrl = String.Format("../ScoreInput/luofang/ExamScoreInput.aspx?classid={0}&causeid={1}", info.RID, info.CauseID);
            scoreLink.Attributes.Add("onclick", "detailView(2);");
            cell.Controls.Add(scoreLink);
            row.Cells.Add(cell);

            tbResult.Rows.Add(row);
        }
    }


    /// <summary>
    /// 构造 班级相关评估 结果表格
    /// </summary>
    private void CreateClassEvalTable(IList<ClsInfo> classInfo, IList<ClassEvalState> stateInfo, int year, int term, DateTime termStart)
    {
        int width = 90;
        TableHeaderRow hrow = new TableHeaderRow();
        TableHeaderCell hcell = new TableHeaderCell();
        hcell.Text = "班级";
        hcell.Style[HtmlTextWriterStyle.Width] = "90px";
        hrow.Cells.Add(hcell);

        IList<DateTime> dateList = new List<DateTime>();
        ListSort<ClassEvalState> sort = new ListSort<ClassEvalState>(stateInfo, "StartTime", false);
        stateInfo = sort.Sort();

        while (!(termStart.Month > DateTime.Now.Month && termStart > DateTime.Now))
        {

            hcell = new TableHeaderCell();
            hcell.Text = String.Format("{0}月", termStart.Month);
            hcell.Style[HtmlTextWriterStyle.Width] = "75px";
            width += 75;
            hrow.Cells.Add(hcell);
            dateList.Add(termStart);
            termStart = termStart.AddMonths(1);
        }
        tbResult.Rows.Add(hrow);

        tbResult.Style[HtmlTextWriterStyle.Width] = String.Format("{0}px", width);

        // 填充表内容
        TableRow row;
        TableCell cell;
        foreach (ClsInfo info in classInfo)
        {
            row = new TableRow();
            cell = new TableCell();
            cell.Text = info.ClsName;
            row.Cells.Add(cell);
            int i = 0;
            foreach (DateTime dt in dateList)
            {
                cell = new TableCell();
                bool sign = false;
                foreach (ClassEvalState state in stateInfo)
                {
                    if (state.ClassID != info.RID || state.StartTime.Month != dt.Month) continue;
                    // 构造超链html标签
                    HyperLink control = new HyperLink();
                    control.Text = "已录入";
                    control.Attributes.Add("onclick", "detailView();");
                    control.NavigateUrl = String.Format("../../../Moral/Teacher/ScoreInput/ClassMonthInput.aspx?SchoolYear={0}&Term={1}&ClsID={2}&evalDate={3}&Type=Edit&Attribute={4}&evalID={5}",
                                                        year, term,
                                                        state.ClassID, state.StartTime.ToString("yyyy-MM-dd"), attribute, evalId);
                    cell.Controls.Add(control);
                    sign = true;
                    break;
                }
                if (!sign)
                {
                    // 构造超链html标签
                    HyperLink control = new HyperLink();
                    control.Text = "未录入";
                    control.Attributes.Add("onclick", "detailView();");
                    control.NavigateUrl = String.Format("../../../Moral/Teacher/ScoreInput/ClassMonthInput.aspx?SchoolYear={0}&Term={1}&ClsID={2}&evalDate={3}&Type=Edit&Attribute={4}&evalID={5}",
                                            year, term,
                                            info.RID, dt.ToString("yyyy-MM-dd"), attribute, evalId);
                    cell.Controls.Add(control);
                }
                cell.Attributes.Add("onmouseout", "mout(" + i + ");");
                cell.Attributes.Add("onmouseover", "mover(" + i + ");");
                row.Cells.Add(cell);
                i++;
            }

            tbResult.Rows.Add(row);
        }
    }

}
